package com.swagger2.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/6/30  9:54
 * 描述:
 */
@Configuration
public class SwaggerUIConfig {

    @Autowired
    private Environment environment;

    @Bean
    public Docket getDocket1(){

        boolean profiles = environment.acceptsProfiles("test", "dev");

        Docket docket=new Docket(DocumentationType.SWAGGER_2);
        docket.apiInfo(getApiInfo())
                .groupName("分组01")
                .enable(profiles)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.swagger2.web"))
                .paths(PathSelectors.ant("/v01/**"))
                .build();


        return docket;
    }

    @Bean
    public Docket getDocket2(){

        boolean profiles = environment.acceptsProfiles("test", "dev");
        Docket docket=new Docket(DocumentationType.SWAGGER_2);
        docket.apiInfo(getApiInfo())
                .groupName("分组02")
                .enable(profiles)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.swagger2.web"))
                .paths(PathSelectors.ant("/v02/**"))
                .build();


        return docket;
    }


    public ApiInfo getApiInfo(){

        Contact contact=new Contact("张三","http://www.baidu.com","zhangsan123@126.com");

        ApiInfoBuilder apiInfoBuilder=new ApiInfoBuilder();
        ApiInfo apiInfo = apiInfoBuilder.description("文档的描述")
                .version("1.0")
                .title("文档的标题")
                .termsOfServiceUrl("http://www.taobao.com")
                .contact(contact)
                .build();

        return apiInfo;
    }

}
